package com.hulk.dryad.activiti7.service.impl;


import com.hulk.dryad.persistence.entity.ActWorkflowFormData;


import com.hulk.dryad.persistence.entity.HistoryData;
import com.hulk.dryad.persistence.entity.HistoryFormData;
import com.hulk.dryad.activiti7.service.IActWorkflowFormDataService;
import com.hulk.dryad.activiti7.service.IFormHistoryDataService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hulk
 * @since 2019-03-19
 */
@AllArgsConstructor
@Service
public class FormHistoryDataServiceImpl implements IFormHistoryDataService {


    private IActWorkflowFormDataService actWorkflowFormDataService;

    private static  final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public List<HistoryData> historyDataShow(String businessKey) {
        List<HistoryData> historyFromDatas=new ArrayList<>();
        List<ActWorkflowFormData> actWorkflowFormData = actWorkflowFormDataService.selectActWorkflowFormDataByBusinessKey(businessKey);
        Map<String, List<ActWorkflowFormData>> collect = actWorkflowFormData.stream().collect(Collectors.groupingBy(ActWorkflowFormData::getTaskNodeName));
        collect.entrySet().forEach(
                entry -> {
                    HistoryData hstoryFromData = new HistoryData();
                    hstoryFromData.setTaskNodeName(entry.getValue().get(0).getTaskNodeName());
                    hstoryFromData.setCreateName(entry.getValue().get(0).getCreatedBy());
                    hstoryFromData.setCreatedDate(sdf.format(entry.getValue().get(0).getCreatedTime()));
                    hstoryFromData.setFormHistoryData(entry.getValue().stream().map(awfd->new HistoryFormData(awfd.getControlName(),awfd.getControlValue())).collect(Collectors.toList()));
                    historyFromDatas.add(hstoryFromData);
                }
        );
        List<HistoryData> collectSort = historyFromDatas.stream().sorted((x, y) -> x.getCreatedDate().compareTo(y.getCreatedDate())).collect(Collectors.toList());

        return collectSort;
    }



}
